package com.alan.wifishare.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.text.TextUtils;

import com.alan.libapi.model.business.SharedWifiInfo;
import com.alan.library.allog.KLog;
import com.alan.wifishare.app.GlobalField;
import com.alan.wifishare.model.business.ScanedWifiInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @className: BusinessUtil
 * @describtion: BusinessUtil
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/5/1 18:51
 */
public class BusinessUtil {

    /**
     * 判断是否APP是否连接上Link设备
     * @return
     */
    public static boolean isConnectedLink(){
        boolean isConnecd = false;
        if(!isConnecd){
            return true;
        }
        // 判断是否连接上Link
        WifiInfo wifiInfo = GlobalField.wifiUtil.getWifiInfo();
        KLog.e("wifiInfo = " + wifiInfo);
        if(GlobalField.userBoundDevice == null || wifiInfo == null){
            return isConnecd;
        }else if(wifiInfo.getSSID().equals("<unknown ssid>")){
            return isConnecd;
        }
        if(wifiInfo.getBSSID().equals(GlobalField.userBoundDevice.getDeviceMac())){
            isConnecd = true;
        }
        KLog.e("isConnecd = " + isConnecd);
        return isConnecd;
    }


    /**
     * 检测是否是手机号
     * @param mobilePhone
     * @return
     */
    public static boolean isMobileNum(String mobilePhone) {
        //检查手机号是否合法//http://www.cnblogs.com/gengaixue/p/4625233.html
        String telRegex = "13\\d{9}|14[57]\\d{8}|15[012356789]\\d{8}|18[01256789]\\d{8}|17[0678]\\d{8}";
        if (TextUtils.isEmpty(mobilePhone)){
            return false;
        }else {
            return mobilePhone.matches(telRegex);
        }
    }


    /**
     * 判断网络是否连接
     * @param context
     * @return
     */
    public static  boolean isNetworkConnected(Context context) {
        ConnectivityManager con = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkinfo = con.getActiveNetworkInfo();
        if (networkinfo == null || !networkinfo.isAvailable()) {
            return false;
        }
        return true;
    }


    /**
     * 检查扫描的wifi是否已被分享过
     * @param wifiScanResultList
     * @param sharedWifiInfoList
     * @return
     */
    public static List<ScanedWifiInfo> checkScanedWifiIsShared(List<ScanResult> wifiScanResultList, List<SharedWifiInfo> sharedWifiInfoList){
        List<ScanedWifiInfo> scanedWifiInfoList = new ArrayList<>();
        HashMap<String, String> sharedWifiHash = new HashMap<>();
        for (SharedWifiInfo sharedWifiInfo : sharedWifiInfoList) {
            sharedWifiHash.put(sharedWifiInfo.getWifiMac(), sharedWifiInfo.getWifiMac());
        }
        for (ScanResult scanResult:wifiScanResultList) {
            ScanedWifiInfo scanedWifi = new ScanedWifiInfo();
            scanedWifi.setSSID(scanResult.SSID);
            scanedWifi.setMac(scanResult.BSSID);
            String wifiMac = sharedWifiHash.get(scanResult.BSSID);
            scanedWifi.setShared(wifiMac == null ? false : true);
            scanedWifiInfoList.add(scanedWifi);
        }
        return scanedWifiInfoList;
    }
}
